package com.exam.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.mapper.AnswerMapper;
import com.exam.mapper.PaperMapper;
import com.exam.service.AnswerService;
import com.exam.vo.AnswerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

@Service
@EnableTransactionManagement(proxyTargetClass = true)
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private PaperMapper paperMapper;

    @Override
    public IPage<AnswerVO> findAll(Page<AnswerVO> page) {
        return answerMapper.findAll(page);
    }

    @Override

    public boolean deleteByIdAndType(Integer questionId, String type) {
        //将类型转换为对应的数据库表名
        String tableName = "";
        //判断类型
        Integer questionType = 0;
        switch (type) {
            case "选择题": tableName = "multi_question";
                            questionType = 1;
             break;
            case "判断题": tableName = "judge_question";
                            questionType = 2;
            break;
            case "填空题": tableName = "fill_question";
                            questionType = 3;
            break;
        }
        //根据类型删除试卷中的题目
        paperMapper.deleteByQuestionAndType(questionType, questionId);
        return answerMapper.deleteByIdAndType(questionId, tableName);
    }
}
